package gui.editor;

import generated.domain.message.Message;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import vworld.Client;

public class PrivateChatEditor extends EditorPart {
	public static String ID = "gui.editor.PrivateChatEditor";
	private Text messagesArea;
	private Text entry;
	private Button sendButton;

	public PrivateChatEditor() {
	}

	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
		setPartName(getUser());
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		layout.numColumns = 2;
		container.setLayout(layout);

		messagesArea = new Text(container, SWT.BORDER | SWT.MULTI | SWT.WRAP);
		messagesArea.setLayoutData(new GridData(GridData.FILL, GridData.FILL,
				true, true, 2, 1));
		messagesArea.setEditable(false);
		messagesArea.setBackground(messagesArea.getDisplay().getSystemColor(
				SWT.COLOR_TITLE_BACKGROUND));
		messagesArea.setForeground(messagesArea.getDisplay().getSystemColor(
				SWT.COLOR_TITLE_FOREGROUND));

		entry = new Text(container, SWT.BORDER | SWT.WRAP);
		GridData gridData = new GridData(GridData.FILL, GridData.FILL, true,
				false);
		gridData.heightHint = entry.getLineHeight() * 2;
		entry.setLayoutData(gridData);
		entry.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent event) {
				if (event.character == SWT.CR) {
					sendMessage();
					// ignore the CR and don't add to text control
					event.doit = false;
				}
			}
		});
		
		sendButton = new Button(container, SWT.BORDER | SWT.WRAP);
		sendButton.setText("Send");
		sendButton.addSelectionListener(new SelectionListener(){
			public void widgetDefaultSelected(SelectionEvent e) {
			}

			public void widgetSelected(SelectionEvent e) {
				sendMessage();
			}
		});
	}

	@Override
	public void setFocus() {
		if (entry != null && !entry.isDisposed()) {
			entry.setFocus();
		}
	}

	private String getUser() {
		return ((PrivateChatEditorInput) getEditorInput()).getName();
	}

	private String renderMessage(String from, String body) {
		if (from == null)
			return body;
		return "<" + from + "> " + body;
	}

	private void scrollToEnd() {
		int n = messagesArea.getCharCount();
		messagesArea.setSelection(n, n);
		messagesArea.showSelection();
	}

	private void sendMessage() {
		String body = entry.getText();
		if (body.length() == 0)
			return;
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date currentdate = new Date();
		String date = formatter.format(currentdate);

		Message message = new Message();
		message.content = body;
		message.date = date;
		message.receiver = getUser();
		message.sender = Client.CONNECTED_USER.avatar.name;

		Client.ROOM_SERVICE
				.sendPersonalMessage(message);

		messagesArea.append(renderMessage(message.sender, body));
		messagesArea.append("\n");
		scrollToEnd();
		entry.setText("");
	}
	
	public void receiveMessage(String sender, String message) {
		if (message.length() == 0)
			return;
		String receivedMessage = renderMessage(sender, message);
		messagesArea.append(receivedMessage);
		messagesArea.append("\n");
		scrollToEnd();
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}
}
